package net.zjitc.controller.admin;

import net.zjitc.common.ServiceResp;
import net.zjitc.pojo.District;
import net.zjitc.pojo.Movie;
import net.zjitc.pojo.Types;
import net.zjitc.service.DistrictService;
import net.zjitc.service.MovieService;
import net.zjitc.service.TypeService;
import net.zjitc.vo.MovieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/admin")
public class MovieController {
    @Autowired
    MovieService movieService;
    @Autowired
    TypeService typeService;
    @Autowired
    DistrictService districtService;

    @RequestMapping(value = "/movie_list")
    public String movie_list(Model model){
        List<Movie> movieList = movieService.findAll();
        sort(model);
        model.addAttribute("movieList",movieList);
        return "/admin/goods_list";
    }

    @RequestMapping("/search")
    @ResponseBody
    public List<Movie> search(MovieVo vo){
        List<Movie> list = movieService.search(vo);
        return list;
    }

    @RequestMapping(value = "/Movie_addUI")
    public String addUI(Model model){
        sort(model);
        return "/admin/goods_add";
    }

    @RequestMapping(value = "/Movie_add")
    @ResponseBody
    public ServiceResp add(@RequestParam(value = "file",required = false)MultipartFile file, HttpServletRequest request, Movie movie){
        if (file != null)
        {
            String path = request.getServletContext().getRealPath("/product_Image/product_list");
            boolean flag = processFile(movie,file,path);
            if (flag != true){
                return ServiceResp.createByERROR("文件上传失败");
            }
        }
        int count = movieService.add(movie);
        if (count>0)
        {
            return ServiceResp.createBySuccess("电影添加成功",0);
        }
        return ServiceResp.createByERROR("电影添加失败",1);
    }

    @RequestMapping(value = "/Movie_updateUI")
    public String updateUI(Model model,int id){
        sort(model);
        Movie movie = movieService.findById(id);
        model.addAttribute("movie",movie);
        return "/admin/goods_update";
    }

    @RequestMapping(value = "/Movie_update")
    @ResponseBody
    public ServiceResp update(@RequestParam(value = "file",required = false)MultipartFile file, HttpServletRequest request, Movie movie){
        if (file != null)
        {
            String path = request.getServletContext().getRealPath("/product_Image/product_list");
            boolean flag = processFile(movie,file,path);
            if (flag != true){
                return ServiceResp.createByERROR("文件修改失败");
            }
        }
        int count = movieService.update(movie);
        if (count>0)
        {
            return ServiceResp.createBySuccess("电影修改成功",0);
        }
        return ServiceResp.createByERROR("电影修改失败",1);
    }


    @RequestMapping("/deleteMovie")
    @ResponseBody
    public ServiceResp delete(Integer id) {
        int resultCount = movieService.deleteMovie(id);
        if (resultCount > 0) {
            return ServiceResp.createBySuccess("电影删除成功",0);
        }
        return ServiceResp.createByERROR("电影删除失败",1);
    }

    public void sort(Model model){
        List<Types> types_list = typeService.findAll();
        List<District> dis_list = districtService.findAll();
        model.addAttribute("t_list", types_list);
        model.addAttribute("d_list",dis_list);
    }

    private boolean processFile(Movie movie, MultipartFile file, String path) {
        if (file != null)
        {
            File dir = new File(path);
            if ((!dir.exists())||(!dir.isDirectory()))
            {
                dir.mkdirs();
            }
            String filename = UUID.randomUUID().toString()+"-"+file.getOriginalFilename();
            File filepath = new File(dir, filename);
            try {
                file.transferTo(filepath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            movie.setImage("/product_Image/product_list/"+filename);
        }
        return true;
    }
}
